package com.sc.VenusQueryBeta.ui.activites.search;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import android.support.v7.widget.Toolbar;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hengjun.kingstar.sdk.base.BasePresenter;
import com.hengjun.kingstar.sdk.base.activity.BaseMVPCompatActivity;
import com.hengjun.kingstar.sdk.utils.ToastUtils;
import com.sc.VenusQueryBeta.R;
import com.sc.VenusQueryBeta.adapter.SearchAdapter;
import com.sc.VenusQueryBeta.adapter.home.SearchActivityListAdapter;
import com.sc.VenusQueryBeta.constant.Constants;
import com.sc.VenusQueryBeta.contract.search.SearchContract;
import com.sc.VenusQueryBeta.model.bean.SearchBean;
import com.sc.VenusQueryBeta.model.bean.home.KeyWordListBean;
import com.sc.VenusQueryBeta.model.bean.home.SearchListBean;
import com.sc.VenusQueryBeta.presenter.search.SearchPresenter;
import com.sc.VenusQueryBeta.ui.activites.details.SearchZiXunActivity;
import com.sc.VenusQueryBeta.ui.activites.details.TestDetailActivity;
import com.sc.VenusQueryBeta.ui.activites.details.ZiXunDetailsActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 搜索頁面.
 */

public class SearchRootActivity extends BaseMVPCompatActivity<SearchContract.SearchPresenter> implements SearchContract.ISearchView, View.OnLayoutChangeListener {

    @BindView(R.id.searchview)
    SearchView searchView;
    @BindView(R.id.rv_search)
    RecyclerView rvSearchData;
    @BindView(R.id.re_men_rv)
    RecyclerView remenRv;
    @BindView(R.id.quxiao)
    TextView quxiao;
    @BindView(R.id.search_back)
    ImageButton searchBack;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.about_kingstar_infornation)
    AppBarLayout aboutKingstarInfornation;
    @BindView(R.id.rl_search)
    LinearLayout rlSearch;

    private SearchAdapter searchAdapter;
    private String[] hot;
    private SearchBean searchBean;
    private List<String> strings;
    private List<SearchBean> keylist;
    private SearchActivityListAdapter hotactivitylistadapter;
    //软件盘弹起后所占高度阀值
    private int keyHeight = 0;
    private int screenHeight;
    private SearchListBean msearchListBean;

    @NonNull
    @Override
    public BasePresenter initPresenter() {
        return SearchPresenter.newInstance();
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter.getKeyWordList();
        showProgressDialog("正在加载");
        mPresenter.search("");

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        //获取屏幕高度
        screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();
        //阀值设置为屏幕高度的1/3
        keyHeight = screenHeight / 3;
        getfocus();

        int id = searchView.getContext().getResources()
                .getIdentifier("android:id/search_src_text", null, null);
        TextView textView = (TextView) searchView.findViewById(id);
        textView.setTextSize(12);// 设置输入字体大小
//        textView.setGravity(Gravity.BOTTOM);// 设置输入字的位置
//        SearchView.SearchAutoComplete textView = (SearchView.SearchAutoComplete) mSearchViewInclude.findViewById(R.id.searchview);
//     textView.setTextSize(3);
//     textView.seth
//        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
        SpannableString spanText = new SpannableString("請輸入關鍵字查詢");
        spanText.setSpan(new AbsoluteSizeSpan(12, true), 0, spanText.length(),
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        searchView.setQueryHint(spanText);

    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }


    /**
     * 点击searchview获取焦点
     */
    private void getfocus() {
        searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus == true) {
                    rvSearchData.setVisibility(View.GONE);
                    remenRv.setVisibility(View.VISIBLE);
                    quxiao.setVisibility(View.VISIBLE);
                } else {
                    quxiao.setVisibility(View.GONE);
                    rvSearchData.setVisibility(View.VISIBLE);
                    remenRv.setVisibility(View.GONE);
                }
            }
        });

        searchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
//                ToastUtils.showToast("close");
                return false;
            }
        });

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                hideKeybord();
                remenRv.setVisibility(View.GONE);//隱藏keyword
                //執行查詢
                showProgressDialog("正在加载");
                mPresenter.search(s);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                if (s.equals("")) {//顯示keyword 关闭searchview
                    remenRv.setVisibility(View.VISIBLE);
                    rvSearchData.setVisibility(View.GONE);
                }
                return false;
            }
        });

        if (hotactivitylistadapter != null) {
            hotactivitylistadapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    if (msearchListBean != null && msearchListBean.getData().size() > 0) {
//                        ToastUtils.showToast("" + msearchListBean.getData().get(position).getCreateTime());
                    } else {
//                        ToastUtils.showToast("sasas");
                    }
                }
            });
        }


    }


    @OnClick({R.id.search_back, R.id.quxiao})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.search_back:
                finish();
                break;
            case R.id.quxiao:
                finish();
                break;
        }
    }

    @Override
    public void KeyWordListBeanDataSuccess(final KeyWordListBean keyWordListBean) {
        searchAdapter = new SearchAdapter(keyWordListBean.getData(), R.layout.item_keyword);
        remenRv.setAdapter(searchAdapter);
        remenRv.setLayoutManager(new GridLayoutManager(mContext, 3));
        //监听item,得到并返回给searchview
        searchAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                ToastUtils.showToast("" + keyWordListBean.getData().get(position).getSystemKeywordContent());
                searchView.setQuery(keyWordListBean.getData().get(position).getSystemKeywordContent(), true);
                hideKeybord();
                remenRv.setVisibility(View.GONE);//隱藏keyword
                //執行查詢
                showProgressDialog("正在加载");
                mPresenter.search(keyWordListBean.getData().get(position).getSystemKeywordContent());
            }
        });
    }

    @Override
    public void showNetworkError() {
        hideProgressDialog();
    }


    @Override
    public void getSearchDataSuccess(SearchListBean searchListBean) {
        if (searchListBean.getData() != null && searchListBean.getData().size() > 0) {//有數據
            msearchListBean = searchListBean;
            hotactivitylistadapter = new SearchActivityListAdapter(R.layout.item_tjzx, searchListBean.getData());
            rvSearchData.setAdapter(hotactivitylistadapter);
            rvSearchData.setLayoutManager(new LinearLayoutManager(mContext));
            rvSearchData.setVisibility(View.VISIBLE);

            hotactivitylistadapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    if (msearchListBean != null && msearchListBean.getData().size() > 0) {
                        Bundle bundle = new Bundle();
                        bundle.putString(Constants.BANNER_DETAIL, msearchListBean.getData().get(position).getDetailInfo());
//                        startNewActivity(SearchZiXunActivity.class, bundle);
                        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                            startNewActivity(TestDetailActivity.class, bundle);
                        } else {
                            startNewActivity(SearchZiXunActivity.class, bundle);
                        }
                    } else {
//                        ToastUtils.showToast("sasas");
                    }
                }
            });
        } else {//沒數據
            ToastUtils.showToast("查無數據");
        }
        hideProgressDialog();

    }

    @Override
    public void onLayoutChange(View v, int left, int top, int right,
                               int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        //现在认为只要控件将Activity向上推的高度超过了1/3屏幕高，就认为软键盘弹起
        if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > keyHeight)) {

            Toast.makeText(SearchRootActivity.this, "监听到软键盘弹起...", Toast.LENGTH_SHORT).show();

        } else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > keyHeight)) {

            Toast.makeText(SearchRootActivity.this, "监听到软件盘关闭...", Toast.LENGTH_SHORT).show();

        }
    }


}
